package com.yidong;

import com.yidong.entity.YiDongObj;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.*;

import java.io.*;

public class FuKuanICT {


    public static String fksm_path = "D:\\code\\Projects\\yidong\\src\\main\\java\\com\\yidongcfg\\ict付款模板\\付款说明.docx";
    public static String fktzs_path = "D:\\code\\Projects\\yidong\\src\\main\\java\\com\\yidongcfg\\ict付款模板\\付款通知书.docx";
    public static String yhzh_path = "D:\\code\\Projects\\yidong\\src\\main\\java\\com\\yidongcfg\\ict付款模板\\银行账户确认函.docx";



    public static String fksm_out = "D:\\code\\Projects\\yidong\\src\\main\\java\\com\\yidongcfg\\付款说明.docx";
    public static String fktzs_out = "D:\\code\\Projects\\yidong\\src\\main\\java\\com\\yidongcfg\\付款通知书.docx";
    public static String yhzh_out = "D:\\code\\Projects\\yidong\\src\\main\\java\\com\\yidongcfg\\银行账户确认函.docx";

    //ict付款方法汇总
    public void ICTfukuan(YiDongObj obj){
        fuKuanShuoMing(obj);
        fuKuanTongZhi(obj);
        yinHangZhangHu(obj);
    }


    /**
     * 付款说明 .docx
     * @param obj
     */
    public void fuKuanShuoMing(YiDongObj obj){
        try (InputStream is = new FileInputStream(fksm_path);
             XWPFDocument document = new XWPFDocument(is)) {

            // 遍历文档中的所有段落
            for (XWPFParagraph paragraph : document.getParagraphs()) {
                for (XWPFRun run : paragraph.getRuns()) {
                    String runText = run.text();

                    //替换
                    runText = runText.replaceAll("%合同名称前%",obj.getHtmcQ());
                    runText = runText.replaceAll("%合同编号前%",obj.getHtbhQ());
                    runText = runText.replaceAll("%合同金额前%",obj.getHtjeQ());
                    runText = runText.replaceAll("%合同名称后%",obj.getHtmcH());
                    runText = runText.replaceAll("%合同编号后%",obj.getHtbhH());
                    runText = runText.replaceAll("%合同金额后%",obj.getHtjeH());
                    runText = runText.replaceAll("%本次付款金额%",obj.getFkje());
                    runText = runText.replaceAll("%日期%",obj.getToday());

                    run.setText(runText, 0);
                }
            }

            // 保存文档到新文件
            try (OutputStream out = new FileOutputStream(fksm_out)) {
                document.write(out);
            }
            System.out.println("文档已更新并保存为: " + fksm_out);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 付款通知书 docx
     * @param obj
     */
    public void fuKuanTongZhi(YiDongObj obj){
        try (InputStream is = new FileInputStream(fktzs_path);
             XWPFDocument document = new XWPFDocument(is)) {

            // 遍历文档中的所有段落
            for (XWPFParagraph paragraph : document.getParagraphs()) {
                for (XWPFRun run : paragraph.getRuns()) {
                    String runText = run.text();

                    //替换
                    runText = runText.replaceAll("%合同名称%",obj.getHtmcQ());
                    runText = runText.replaceAll("%合同编号%",obj.getHtbhfk());
                    runText = runText.replaceAll("%项目编号%",obj.getXmbh());
                    runText = runText.replaceAll("%合同金额%",obj.getHtzjefk());
                    runText = runText.replaceAll("%本次付款金额%",obj.getFkje());
                    runText = runText.replaceAll("%合同名称前%",obj.getHtmcQ());
                    runText = runText.replaceAll("%业主名称%",obj.getYzmc());
                    runText = runText.replaceAll("%前向合同收款条件%",obj.getSktjQ());


                    run.setText(runText, 0);
                }
            }

            // 保存文档到新文件
            try (OutputStream out = new FileOutputStream(fktzs_out)) {
                document.write(out);
            }
            System.out.println("文档已更新并保存为: " + fktzs_out);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 银行账户确认函 docx
     * @param obj
     */
    public void yinHangZhangHu(YiDongObj obj){
        try (InputStream is = new FileInputStream(yhzh_path);
             XWPFDocument document = new XWPFDocument(is)) {

            // 遍历文档中的所有段落
            for (XWPFParagraph paragraph : document.getParagraphs()) {
                for (XWPFRun run : paragraph.getRuns()) {
                    String runText = run.text();

                    //替换
                    runText = runText.replaceAll("%合同名称%",obj.getHtmcQ());
                    runText = runText.replaceAll("%日期%",obj.getToday());

                    run.setText(runText, 0);
                }
            }

            // 保存文档到新文件
            try (OutputStream out = new FileOutputStream(yhzh_out)) {
                document.write(out);
            }
            System.out.println("文档已更新并保存为: " + yhzh_out);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
